<template>
<div >
  <div style="width: 50px;height: 50px">

  </div>
  <div  style="margin: 0px 300px">
    <el-form :model="form" label-width="100px" style="padding-right: 50px" :rules="rules" ref="formRef">
      <el-form-item label="标题" prop="articleTitle">
        <el-input v-model="form.articleTitle" placeholder="标题"></el-input>
      </el-form-item>
      <!--    <el-form-item label="简介" prop="descr">
            <el-input type="textarea" v-model="form.descr" placeholder="简介"></el-input>
          </el-form-item>-->
      <el-form-item label="内容" prop="articleContent">
        <div id="editor"></div>
      </el-form-item>
      <el-form-item label="封面" prop="articleCover">
        <el-upload
            :action="'http://localhost:8800/file/upload'"
            :headers="{ token: user.token }"
            list-type="picture"
            :on-success="handleCoverSuccess"
        >
          <el-button type="primary">上传封面</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item label="分类" prop="sortId">
        <el-select v-model="form.sortId"  transfer="true"  :popper-append-to-body="false" style="width: 100%">
          <el-option v-for="(item,index) in categoryList" :key="item.id" :value="index" :label="item"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标签" prop="tags">
        <el-select v-model="tagsArr" multiple filterable allow-create default-first-option style="width: 100%">
          <el-option value="后端"></el-option>
          <el-option value="Java"></el-option>
          <el-option value="面试"></el-option>
          <el-option value="Vue"></el-option>
          <el-option value="前端"></el-option>
          <el-option value="大数据"></el-option>
          <el-option value="算法"></el-option>
          <el-option value="程序员"></el-option>
          <el-option value="小白"></el-option>
        </el-select>
      </el-form-item>

    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button >取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </div>



</div>
</template>

<script>
import E from "wangeditor"
import hljs from 'highlight.js'
export default {
  name: "AddArticle",
  data(){
    return{
      title: null,
      categoryName: null,
      form: {},
      userName: null,
      fromVisible: false,
      user: JSON.parse(localStorage.getItem('news') || '{}'),
      ids: [],
      categoryList: ['大数据','前端','人工智能','文学','科技'],
      tagsArr: [],
      editor: null,
      rules: {
        articleTitle: [
          { required: true, message: "请输入标题", trigger: "blur" },
        ],
        articleContent: [
          { required: true, message: "请输入内容", trigger: "blur" },
        ],
        sortId: [
          { required: true, message: "请选择分类", trigger: "blur" },
        ],
      },
    }

  },
  created() {
    this.setRichText()
  },

  mounted () {
    window.onbeforeunload = function (e) {
      return ''
    }
  },
  beforeRouteLeave (to, from, next) {
    if (this.progressVisible) {
      this.$confirm('系统可能不会保存您所做的更改。', '离开此页面？', {
        confirmButtonText: '离开',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        next()
      })
    } else {
      next()
    }
  },
  beforeDestory () {
    window.onbeforeunload = null
  },

  methods:{
    setRichText() {
      this.$nextTick(() => {
        this.editor = new E(`#editor`)
        this.editor.highlight = hljs
        this.editor.config.uploadImgServer = 'http://localhost:8800/file/editor/upload'
        this.editor.config.uploadFileName = 'file'
        this.editor.config.uploadImgHeaders = {
          token: this.user.token
        }
        this.editor.config.uploadImgParams = {
          type: 'img',
        }
        this.editor.create()  // 创建
      })
    },

    save() {   // 保存按钮触发的逻辑  它会触发新增或者更新
      this.form.tags = JSON.stringify(this.tagsArr)  // 把json数组转换成json字符串存到数据库
      this.form.articleContent = this.editor.txt.html()
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          console.log(this.form)
          this.$request.post('/boke/article/add', this.form).then(res => {
            if (res.code === 200) {  // 表示成功保存
              this.$message.success('保存成功')
              //跳转到自己已发布的的页面
              this.$router.push('/myArticle')
            } else {
              this.$message.error(res.msg)  // 弹出错误的信息
            }
          })
        }
      })
    },
    handleAdd() {   // 新增数据
      this.form = {}  // 新增数据的时候清空数据
      this.setRichText()
      this.fromVisible = true   // 打开弹窗
    },
    handleEdit(row) {   // 编辑数据
      this.form = JSON.parse(JSON.stringify(row))  // 给form对象赋值  注意要深拷贝数据
      this.fromVisible = true   // 打开弹窗
      this.setRichText()
      setTimeout(() => {
        this.editor.txt.html(this.form.content)
      }, 0)
    },
    handleCoverSuccess(res) {
      this.form.articleCover = res.data
    },
    skipTop(){

    }
  }
}
</script>


<style scoped>
.dialog-footer{
  display: flex;
  justify-content: center;
  text-align: center;
}
</style>